/**
 *    Copyright 2012 Geensoft S.A.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
package ar.com.greensoft.neo.dao.impl;

import java.util.List;

import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import ar.com.greensoft.neo.dao.DaoException;
import ar.com.greensoft.neo.dao.PersonaFisicaDao;
import ar.com.greensoft.neo.model.PersonaFisica;
import ar.com.greensoft.neo.model.PersonaFisica_;
import ar.com.greensoft.neo.model.references.Sexo;
import ar.com.greensoft.neo.search.SearchInput;
import ar.com.greensoft.neo.search.user.PersonaFisicaSearchInput;
import ar.com.greensoft.neo.util.SearchUtil;

/**
 * @author Jorge E. Villaverde
 *
 */
@Transactional
@Repository
public class JpaPersonaFisicaDaoImpl extends AbstractJpaWithRevisionDaoImpl<PersonaFisica> implements PersonaFisicaDao {

	@Override
	protected Class<PersonaFisica> getEntityClazz() {
		return PersonaFisica.class;
	}

	@Override
	protected String getCountNamedQuery() {
		return PersonaFisica.COUNT_QUERY_NAME;
	}

	@Override
	protected String getListNamedQuery() {
		return PersonaFisica.FIND_ALL_QUERY_NAME;
	}

	@Override
	public List<PersonaFisica> findBySearchInput(SearchInput input) {
		return null;
	}

	@Override
	protected Expression<Boolean> createWhereFromSearchInput(SearchInput input, Root<PersonaFisica> root) {
		PersonaFisicaSearchInput pfsi = (PersonaFisicaSearchInput)input;
		
		CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder();
		Predicate predicate = null;
		Predicate predDNI = null;
		Predicate predNombre = null;
		Predicate predSexo = null;
		
		if(pfsi.isBuscarDocumento() && pfsi.getTipoDocumento() != null && pfsi.getNroDoc() > 0){
			predDNI = criteriaBuilder.and(
					criteriaBuilder.equal(root.get(PersonaFisica_.tipoDocumento), pfsi.getTipoDocumento()),
					criteriaBuilder.equal(root.get(PersonaFisica_.numeroDocumento), pfsi.getNroDoc()));
			predicate = predDNI;
		}		

		if(pfsi.isBuscarNombre() && StringUtils.hasText(pfsi.getNombre())){
			String searchText = SearchUtil.likeString(pfsi.getNombre());
			predNombre = criteriaBuilder.or(
					criteriaBuilder.like(root.get(PersonaFisica_.primerNombre), searchText),
					criteriaBuilder.like(root.get(PersonaFisica_.segundoNombre), searchText),
					criteriaBuilder.like(root.get(PersonaFisica_.apellido1), searchText),
					criteriaBuilder.like(root.get(PersonaFisica_.apellido2), searchText));
			
			if(predicate == null){
				predicate = predNombre;
			}else{
				predicate = criteriaBuilder.and(predicate, predNombre);
			}
		}
		
		if(pfsi.isBuscarSexo() && pfsi.getSexo() != null){
			predSexo = criteriaBuilder.equal(root.get(PersonaFisica_.sexo), 
					pfsi.getSexo());
			
			if(predicate == null){
				predicate = predSexo;
			}else{
				predicate = criteriaBuilder.and(predicate, predSexo);
			}
		}
		
		return predicate;
	}

	@Override
	public List<PersonaFisica> findBySexo(Sexo sexo) throws DaoException {
		if(logger.isTraceEnabled())
			logger.trace("Returning Personas Fisicas by Sexo: " + sexo);

		TypedQuery<PersonaFisica> query = getEntityManager().createNamedQuery(PersonaFisica.FIND_BY_SEXO_QUERY_NAME, PersonaFisica.class);

		query.setParameter("sexo", sexo);
		
		return query.getResultList();
	}

}
